<?php


namespace app\api\controller\v17;


use app\api\controller\v2\Auth;
use app\common\cachemodel\Fortune;
use app\common\cachemodel\NatalGoods;
use app\common\cachemodel\SystemConfig;
use app\common\cachemodel\ZhunleActivity;
use app\common\lib\ConfigManage;
use app\common\lib\exception\ApiException;
use app\common\lib\Natal;
use app\common\model\Recode;
use app\common\model\Recode as RecodeModel;

/**
 * @version 2.0.0
 * 本命报告和视频详情
 * Class NatalReport
 * @package app\api\controller\v17
 */
class NatalReport extends Auth
{
    /**
     * 本命预测
     * @return string
     * @throws ApiException
     * @throws \Exception
     */
    public function index()
    {
        $rid = input('rid/d', 0);
        $uid = $this->user['id'];

        $recodeModel = new Recode();
        $NatalReportModel = new \app\common\cachemodel\NatalReport();
        if ($rid)
        {
            $recodeInfo = $recodeModel->getRecode(['uid' => $this->user['id'], 'id' => $rid]);
            if (!$recodeInfo)
            {
                return show(config('code.error'), '档案不存在');
            }
        } else
        {
            $recodeInfo = $recodeModel->getSelfRecode(['uid' => $this->user['id']]);
            if (!$recodeInfo)
            {
                return show(config('code.error'), '档案不存在');
            }
            $rid = $recodeInfo['id'];
        }
        //  获取系统配置
        $config = (new SystemConfig())->getSystemConfig(['title1', 'title_intro1', 'title2', 'title_intro2']);
        //  获取分数及词条
        $natal = (new Natal())->getScoreAndText($recodeInfo);
        //  获取本命预测商品信息
        $goodData = (new NatalGoods())->getGoods();
        // 获取解锁订单信息状态
        $unlockReport = $NatalReportModel->getNatalReport2($recodeInfo);
        // $unlockReport = $NatalReportModel->getNatalReport($uid, $rid);

        $forecast = [];//报告数据
        //长图排序
        $large_unlocking = [];
        $large_active = [];
        $large_unopen = [];
        $large_lock = [];
        $large_unlock = [];
        //短图排序
        $small_unlocking = [];
        $small_active = [];
        $small_unopen = [];
        $small_lock = [];
        $small_unlock = [];
        $bar_sort_index = [];//柱状图排序
        $orderModel = new \app\common\cachemodel\Orders();
        $activingModel = new ZhunleActivity();
        $oldNatalScore = ['feelings' => 'love', 'dating' => 'home', 'carrer' => 'work', 'money' => 'money', 'academic' => 'study'];
        //获取报告商品图片
        $reportImage = ConfigManage::natalReportGoodImage(1);
        foreach (array_values($goodData) as $key => $item)
        {
            if ($item['status'] == 2)//暂不开放状态
            {
                $status = 3;
            } else//判断商品解锁状态
            {
                if (isset($unlockReport[$item['id']]))
                {
                    $unlock_status = (int)$unlockReport[$item['id']]['status'] === 2 ? 2 : 1;
                    $status = isset($unlockReport[$item['id']]) ? ($unlock_status) : 0;
                } else
                {
                    $status = 0;
                }
            }
            //获取活动商品信息
            $activing = $activingModel->getActivingGood(7, $item['id']);
            $is_large = 0;
            //判断当前图是否为长图
            if (in_array($item['flag'], $reportImage['large_goods']))
            {
                $is_large = 1;
                $bg_img = $reportImage['large'][$item['flag']];
                if ($activing)
                {
                    $actiGids = explode(',', $activing['gids']);
                    if (in_array($item['id'], $actiGids, false))
                    {
                        $bg_img = $reportImage['large_active'][$item['flag']];
                    }
                }
            } else
            {
                $bg_img = $reportImage['small'][$item['flag']];
                if ($activing)
                {
                    $actiGids = explode(',', $activing['gids']);
                    if (in_array($item['id'], $actiGids, false))
                    {
                        $bg_img = $reportImage['small_active'][$item['flag']];
                    }
                }
            }
            $d = [
                'gid' => (int)$item['id'],
                'score' => $natal[$oldNatalScore[$item['flag']]] ?? 0,
                'text' => $natal[$oldNatalScore[$item['flag']] . '_text'] ?? '',
                'status' => $status,
                'alert_bg_img1' => $item['alert_bg_img1'] ?? '',
                'alert_bg_img2' => $item['alert_bg_img2'] ?? '',
                'bg_img' => $bg_img,
                'title_img' => $item['title_img'] ?? '',
                'unlock_num' => $item['base_num'] + $orderModel->getTotalPrepaidOrder(7, $item['id']),
                'intro' => $item['intro'] ?? '',
                'gcolor' => $item['color'] ?? '',
                'gname' => $NatalReportModel->gidName[$item['flag']] ?: '',
//                'active_time' => $item['active_time']
                'active_time' => 0
            ];

            //长图
            if ($is_large)
            {
                if ($status == 1)
                {   // 解锁中
                    $large_unlocking[] = $key;
                } elseif ($status == 0 && $d['active_time'] > 0)
                {   // 限时特价 并且 未解锁
                    $large_active[] = $key;
                } elseif (!$status)
                {   // 未解锁
                    $large_lock[] = $key;
                } elseif ($status == 3)
                {
                    //未开放
                    $large_unopen[] = $key;
                } else
                {   // 已解锁
                    $large_unlock[] = $key;
                }
            } //短图
            else
            {
                if ($status == 1)
                {   // 解锁中
                    $small_unlocking[] = $key;
                } elseif ($status == 0 && $d['active_time'] > 0)
                {   // 限时特价 并且 未解锁
                    $small_active[] = $key;
                } elseif (!$status)
                {   // 未解锁
                    $small_lock[] = $key;
                } elseif ($status == 3)
                {
                    //未开放
                    $small_unopen[] = $key;
                } else
                {   // 已解锁
                    $small_unlock[] = $key;
                }
            }
            //柱状图
            $bar_sort_index[] = $key;
            //报告数据
            $forecast[] = $d;
        }

        $data = [
            'page_title' => '一生报告',
            'title1' => (isset($config['title1']) && !empty($config['title1'])) ? $config['title1'] : '',
            'title_intro1' => (isset($config['title_intro1']) && !empty($config['title_intro1'])) ? $config['title_intro1'] : '',
            'title2' => (isset($config['title2']) && !empty($config['title2'])) ? $config['title2'] : '',
            'title_intro2' => (isset($config['title_intro2']) && !empty($config['title_intro2'])) ? $config['title_intro2'] : '',
            'forecast_list' => $forecast,
            'large_sort_index' => array_merge($large_unlocking, $large_unlock, $large_active, $large_lock, $large_unopen),
            'small_sort_index' => array_merge($small_unlocking, $small_unlock, $small_active, $small_lock, $small_unopen),
            'bar_sort_index' => $bar_sort_index
        ];
        return show(config('code.success'), '一生报告', $data);
    }

    /**
     * 获取不同运势的信息
     * @throws ApiException
     * @throws \Exception
     */
    public function multipleFortune()
    {
        $type = input('type/d', 0);
        $in_sign = input('in_sign/d', 1);
        $fortuneModel = new Fortune();
        switch ($type)
        {
            case 1:
                $data = $fortuneModel->todayInfo($this->user);
                break;
            case 2:
                if ($this->user && $this->user['rid'])
                {
                    $map = ['uid' => $this->user['id'], 'isself' => 1];
                    $recode_info = (new RecodeModel())->getSelfRecode($map);

                    if (!$recode_info || !$recode_info['sun'])
                    {
                        return show(406, '档案缺失请添加档案');
                    }
                    $in_sign = explode('-', $recode_info['sun'])[0];
                } elseif (!in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
                {
                    return show(408, '请选择星座');
                }
                $data = $fortuneModel->weekInfo($in_sign);
                break;
            case 3:
                $data = $fortuneModel->monthInfo($this->user, $this->headers['version']);
                break;
            case 4:
                $data = $fortuneModel->yearInfo($this->user, $this->headers['version']);
                break;
            case 5:
                $data = $fortuneModel->reportInfo($this->user);
                break;
            default:
                $data = [];
                break;
        }
        if ($data)
        {
            return show(config('code.success'), '获取成功', $data);
        }
        return show(config('code.error'), '未知的运势');
    }
}
